package com.example.demo.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.dao.CollegeDao;
import com.example.demo.pojo.ResultVo;
import com.example.demo.pojo.SearchDto;
import com.example.demo.pojo.TbCollege;
import com.example.demo.service.CollegeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class CollegeServiceImpl extends ServiceImpl<CollegeDao, TbCollege> implements CollegeService {

    @Autowired
    private CollegeDao collegeDao;

    @Override
    public ResultVo saveCollege(TbCollege tbCollege) {

//        QueryWrapper<TbCollege> query=new QueryWrapper<>();
//        query.eq("name",tbCollege.getName());

        LambdaQueryWrapper<TbCollege> lambdaQueryWrapper = new LambdaQueryWrapper<>();
lambdaQueryWrapper.eq(TbCollege::getName,tbCollege.getName());
        //添加之前需要判断方向是否存在
        int count=collegeDao.selectCount(lambdaQueryWrapper);
        if (count == 0) {
            //代表方向存在
            collegeDao.insert(tbCollege);
            return new ResultVo(200,"添加成功",null);
        }else {
            //方向不存在  不添加

            return new ResultVo(201,"名称存在，不允许添加",null );
        }


    }

//    状态查询

    @Override
    public List<TbCollege> search(int status) {
        LambdaQueryWrapper<TbCollege> query = new LambdaQueryWrapper<>();

           query.eq(TbCollege::getStatus,status);
        List<TbCollege> list=collegeDao.selectList(query);


        return list;
    }
//组合查询
    @Override
    public List<TbCollege> searchvo(TbCollege tbCollege) {
        LambdaQueryWrapper<TbCollege> query = new LambdaQueryWrapper<>();
        if (!StringUtils.isEmpty(tbCollege.getName())){
            query.like(TbCollege::getName,tbCollege.getName());
        }

        if (tbCollege.getStatus()!=0){
            query.eq(TbCollege::getStatus,tbCollege.getStatus());
        }


        List<TbCollege> list= collegeDao.selectList(query);

        return list;
    }
//    分页加模糊查询
    @Override
    public IPage<TbCollege> searchPage(SearchDto searchDto) {
        IPage<TbCollege> page = new Page<TbCollege>(searchDto.getPage(),searchDto.getSize());

        LambdaQueryWrapper<TbCollege> query = new LambdaQueryWrapper<>();

        if (!StringUtils.isEmpty(searchDto.getName())){
            query.like(TbCollege::getName,searchDto.getName());
        }

        if (searchDto.getStatus()!=0){
            query.eq(TbCollege::getStatus,searchDto.getStatus());
        }

       IPage<TbCollege> list= collegeDao.selectPage(page,query);


        return list;
    }
}
